//获取应用实例
const app = getApp()

Page({
  data: {
    indicator_dots: true,
    indicator_color: "#ffffff",
    indicator_active_color: "#F8626E",
    userInfo:{},
    autoplay: true,
    interval: 3000,
    circular: true,
    ava:null,
    curCommunityName:null,
    curCommunityStatu:null,
    communityList:[],
    functionList: [{
       "title": "社区公告",
        "url": "notice",
        "name": "channel"
      },
      {
        "title": "信息发布",
        "url": "post",
        "name": "pinglunhuifu"
      },
      {
        "title": "社区警务",
        "url": "chat",
        "name": "anquan"
      },
      {
        "title": "党建活动",
        "url": "cpc",
        "name": "jiangzhang"
      },
      {
        "title": "物业缴费",
        "url": "payFirst",
        "name": "feiyong"
      },
      {
        "title": "在线报修",
        "url": "repair",
        "name": "tune_up"
      },
      {
        "title": "家有老小",
        "url": "elderly",
        "name": "aixin3"
      },
      {
        "title": "业委会",
        "url": "owner",
        "name": "yudinghuiyishi"
      },
      {
        "title": "物业服务",
        "url": "wyservice",
        "name": "fuwuxiangmu"
      },
      {
        "title": "投诉建议",
        "url": "complain",
        "name": "complain-fill"
      }
      ,
      {
        "title":"社区医院",
        "url":"hospital",
        "name": "yiyuan"
      }

    ],
    activities: [{
        "title": "社区庆五一活动",
        "address": "社区物业中心",
        "endTime": "2021-4-30",
        "url": "https://z3.ax1x.com/2021/05/06/gQWCIx.jpg",
        "notice_id": '20210428101'
      },
      {
        "title": "社区乒乓球比赛",
        "address": "社区物业中心",
        "endTime": "2021-5-19",
        "url": "https://z3.ax1x.com/2021/05/06/gl0qs0.jpg",
        "notice_id": '20210510101'
      }
    ],
    // 广告地址
    advert: [],
    // 是否显示中间广告
    adCenter: true,
    houseList: []
  },
  onNavTap(e) {
    var url = e.currentTarget.dataset.url
    if(url=="cpc"){
      app.sendGetRequest('/community/cpc/member/ifCpc/'+this.data.userInfo.userId, (res) => {
        // console.log('是否为党员', res)
        if(res){
          wx.navigateTo({
            url: '/pages/cpc/cpc',
          })
        }else{
          wx.showModal({
            title: '提醒',
            content: '当前账号非党员身份，无法访问党建模块！请联系管理员处理',
            showCancel: false,
            success (res) {
              if (res.confirm) {
                // console.log('用户点击确定')
              }
            }
          })
        }
      }, (err) => {
        console.log(err.errMsg)
      })
    }else{
      wx.navigateTo({
        url: '/pages/'+url+'/'+url,
      })
    }
  },
  goToShop() {
    wx.navigateToMiniProgram({
      appId: 'wx43e4b03cfcb57b07',
      path: 'pages/index/index',
      envVersion: 'trial',
      extraData: {},
      success(res) {
        // 打开成功
        console.log("打开成功")
      }
    })
  },
  goToActivity: function (e) {
    wx.navigateTo({
      url: 'url',
    })
  },
  goToActivity: function (e) {
    wx.navigateTo({
      url: '../notice/single_notice/single_notice?id=' + e.currentTarget.dataset.notice_id,
    })
  },

  onLoad:async function () {
    // 需要登录后请求的接口放到 successLogin， 放到这里因为异步返回 401 会重复登录
    // this.login();
    // console.log("onload")
    // if(app.globalData.employ && app.globalData.employ!=''){
    //   console.log("上来就试")
    //   this.setCommunity();
    //   this.setHouse();
    // }else{
    //   app.employCallback = employ => {
    //       if(employ != ''){
    //         console.log("非首次尝试")
    //         this.setCommunity();
    //          this.setHouse();
    //       }
    //   }
    // }
    let token = app.getToken();
    if(token == ''){
      console.log("没有token")
      app.tokenCallback = (token)=>{
        if(token != ''){
          app.setToken(token)
          this.setCommunity();
           this.setHouse();
           this.successLogin();
        }
      }
    }else{
      this.setCommunity();
      this.setHouse();
      this.successLogin();
    }
    
  },
  

  successLogin(res) {
    var that = this
    // 取消 loading
    wx.hideLoading()
    
    // wx.showToast({
    //   title: '登录成功',
    //   duration: 2000
    // })
    // 登录成功后获取用户信息
    app.sendGetRequest('/wxGetInfo', (res) => {
      app.setUserInfo(res.user)
      that.setData({
        userInfo:res.user
      })
      console.log(that.data.userInfo)
     
      var cid = res.user.communityId
      for(var i=0;i<that.data.communityList.length;i++){
        if(that.data.communityList[i].communityId==cid ){
          that.setData({
            curCommunityName:that.data.communityList[i].name
          })
        }
      }
    })
    this.isFirstEnter();
    // 获取广告地址
    this.getAdvertUrl();
    // 首次登录添加积分
    app.sendGetRequest('/system/details/sign', res => {
      if(res.code == 200) {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
 
  setCommunity(){
    var that = this
// 获取社区列表 
    // wx.request({
    //   url: app.globalData.base_request+'/system/community/list/all',
    //   method:'GET',
    //   header:{
    //     'content-type': 'application/json',
    //   },
    //   success:function(res){
    //     wx.setStorageSync('communityList', res.rows)
    //     that.setData({
    //       communityList: res.rows
    //     })
    //   },
    //   fail: function(err) {
    //     // errFun(err)
    //     console.log(err)
    //   }
    // })
    app.sendGetRequest('/system/community/list/all',(res)=>{
      wx.setStorageSync('communityList', res.rows)
      that.setData({
        communityList: res.rows
      })
    },(err)=>{
      console.log(err)
    })
    
    wx.getStorage({
      key:'communityList',
      success:function(res){
        that.setData({
          communityList: res.data
        })
      }
    })
  },
  gotoChangeCommunity(){
    wx.navigateTo({
      url: '../../pages/me/changeCommunity/changeCommunity'
    })
  },
  gotoCommunityHospital(){    wx.navigateTo({
      url: '../../pages/me/info/info'
    })
  },
  relogin(){
    var that = this
    wx.getStorage({
      key:'userInfo',
      success:function(res){
        var data = res.data
        wx.login({
          success(res){
            data["code"] = res.code
            that.doLogin(data)
          }
        })
      }
    })
  },
  gotoCheckInfo(){
    wx.navigateTo({
      url: '../../pages/me/info/info'
    })
  },
  method: {
    exitScreen: function(e) {

    }
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
  },
  // 获取广告地址
  getAdvertUrl() {
    app.sendGetRequest('/community/adposcomm/list/community', res => {
      console.log('广告位置', res.data)
      this.advert = res.data;
      this.setData({
        advert: this.advert
      })
    })
  },
  // 判断是否第一次登陆小程序
  isFirstEnter() {
    this.setData({
      firstEnter: app.globalData.firstEnter
    })
  },
  // 关闭首页弹窗广告
  close() {
    this.setData({
      firstEnter: false
    })
    app.globalData.firstEnter = false
  },
  // 关闭中间广告
  adCenterClose() {
    this.adCenter = false;
    this.setData({
      adCenter: this.adCenter
    })
  },

  setHouse() {
    var that = this
    app.sendGetRequest('/system/house/user',(res)=>{
      console.log("获取认证房屋", res)
      var houseList = []
      that.setData({
        houseList: res.data
      })
    },(err)=>{
      console.log(err)
    })
  }
})